package com.lzjn.code.utils;

import com.lzjn.code.common.util.FieldUtil;
import com.lzjn.code.web.entity.DataSource;
import com.lzjn.code.web.entity.TableConfig;
import com.lzjn.code.web.entity.TableFieldConfig;
import com.lzjn.code.web.util.ReadTableUtil;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class ExportTableInfoUtil {

    private static final String[] tableNames = {
            "limit_work",
            "limit_work_annex",
            "limit_work_approve_record",
            "limit_work_gas"
    };

    @GetMapping("/exportTableInfo")
    public String exportTableInfo(HttpServletResponse response) {

        ReadTableUtil readTableUtil = new ReadTableUtil();

        try {
            DataSource dataSource = new DataSource();
            dataSource.setDbType("mysql");
            dataSource.setServer("10.198.40.77");
            dataSource.setPort("3306");
            dataSource.setDbName("hussar-base-cloud");
            dataSource.setUsername("root");
            dataSource.setPassword("1yrVZP%KqZ#SS_cscec81_Z#98");

            List<String> tableList = readTableUtil.readAllTableNames(dataSource);

            List<String> tableNameList = Arrays.asList(tableNames);

            List<TableConfig> tableConfigs = new ArrayList<>();

            for(String tableName : tableList) {

                if(tableNameList.contains(tableName)) {
                    TableConfig tableConfig = new TableConfig();
                    String description = readTableUtil.readTableCommentByName(dataSource, tableName);
                    tableConfig.setTableName(tableName);
                    tableConfig.setDescription(description);
                    tableConfig.setEntityName(FieldUtil.formatFieldCapital(tableName));
                    List<TableFieldConfig> fields =  readTableUtil.readTableColumn(dataSource, tableName);
                    fields.stream().forEach(field -> {
                        if("varchar".equals(field.getFieldType())) {
                            field.setFieldType(field.getFieldType()+"("+field.getFieldLength()+")");
                        }
                    });
                    tableConfig.setFieldConfigs(fields);
                    tableConfigs.add(tableConfig);
                }
            }

            Context context = new Context();
            context.putVar("tableConfigs", tableConfigs);

            Resource resource =  new ClassPathResource("generator/excel/db_schema_jxls.xlsx");
            InputStream in = resource.getInputStream();

            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + dataSource.getDbName() + ".xlsx");
            OutputStream out = response.getOutputStream();

            JxlsHelper.getInstance().processTemplate(in, out, context);

            in.close();
            out.close();

            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json;charset=utf-8");
            return "fail";
        }
    }

}
